﻿<Window x:Class="SubmitCommandWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ex="clr-namespace:SubmitCommandExample"
    xmlns:v="http://wpfglue.wordpress.com/validation"
    Title="SubmitCommandWindow" Width="250" Height="350">
    <Window.Resources>
        <ex:Customer x:Key="data"/>
        <RoutedCommand x:Key="SubmitCommand"/>
        <Style x:Key="submitCommandStyle" TargetType="GroupBox">
            <Setter Property="v:Submit.Command" Value="{StaticResource SubmitCommand}"/>
            <Setter Property="v:Submit.KeyGesture" Value="Enter"/>
        </Style>
    </Window.Resources>
    <StackPanel DataContext="{StaticResource data}">
    <!-- CustomerGroupBox is the base for the SubmitCommand Sticky Command. 
        All controls inside this container will connect the Enter key with the SubmitCommand.
        Because Submit.Command sets CustomerGroupBox.BindingGroup automatically, 
        the UpdateSourceTrigger for all controls will be changed to Explicit, 
        and they will update their sources together when the SubmitCommand is invoked.
        
        All Buttons inside this container will be able to send out the SubmitCommand
        without a CommandTarget parameter.-->
        <GroupBox x:Name="CustomerGroupBox" Header="Customer" Style="{StaticResource submitCommandStyle}">
                  <!--v:Submit.Command="{StaticResource SubmitCommand}" 
                  v:Submit.KeyGesture="Enter">-->
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Label Grid.Row="0">Name</Label>
                <Label Grid.Row="1">Address</Label>
                <Label Grid.Row="2">City</Label>
                <Label Grid.Row="3">Country</Label>
                <TextBox x:Name="NameTextBox" Grid.Column="1" Grid.Row="0" 
                         Text="{Binding Name}"/>
                <TextBox x:Name="AddressTextBox" Grid.Column="1" Grid.Row="1" 
                         Text="{Binding Address}"/>
                <TextBox x:Name="CityTextBox" Grid.Column="1" Grid.Row="2" 
                         Text="{Binding City}"/>
                <TextBox x:Name="CountryTextBox" Grid.Column="1" Grid.Row="3" 
                         Text="{Binding Country}"/>
                <Button Command="{StaticResource SubmitCommand}"
                        Grid.Row="4">Submit</Button>
            </Grid>
        </GroupBox>

        <GroupBox Header="Customer Data">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Label Grid.Row="0">Name</Label>
                <Label Grid.Row="1">Address</Label>
                <Label Grid.Row="2">City</Label>
                <Label Grid.Row="3">Country</Label>
                <TextBlock x:Name="NameTextBlock" Grid.Column="1" Grid.Row="0" Text="{Binding Name}"/>
                <TextBlock x:Name="AddressTextBlock" Grid.Column="1" Grid.Row="1" Text="{Binding Address}"/>
                <TextBlock x:Name="CityTextBlock" Grid.Column="1" Grid.Row="2" Text="{Binding City}"/>
                <TextBlock x:Name="CountryTextBlock" Grid.Column="1" Grid.Row="3" Text="{Binding Country}"/>
            </Grid>
        </GroupBox>
        <!-- This Button invokes the SubmitCommand on the CustomerGroupBox 
             directly by setting it as CommandTarget.-->
        <Button Command="{StaticResource SubmitCommand}" CommandTarget="{Binding ElementName=CustomerGroupBox}">Submit</Button>
    </StackPanel>
</Window>
